home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / PhaseShiftX / Source / FinderPatch / FinderPatch.cp next >
Encoding:
Text File  |  2001-06-23  |  4.4 KB  |  161 lines

  1. #include <ApplicationServices/ApplicationServices.h>
  2. #include <Carbon/Carbon.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "FinderPatch.h"
  6. #include "MachOUtils.h"
  7.  
  8.  
  9.  
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13.  
  14. typedef UInt32    CGSConnectionID;
  15.  
  16. extern CGSConnectionID _CGSDefaultConnection(void);
  17. extern OSStatus CGSSetWindowOpacity(CGSConnectionID connID,CGSConnectionID winID,int isOpaque);
  18. extern OSStatus CGSSetWindowAlpha(CGSConnectionID connID,CGSConnectionID winID,float alpha);
  19. extern OSStatus CGSLockWindowBits(CGSConnectionID connID,CGSConnectionID winID,CGRect *bounds,UInt32 *depth,void *data,SInt32 *rows);
  20. extern OSStatus CGSUnlockWindowBits(CGSConnectionID connID,CGSConnectionID winID,void *updateRgn);
  21.  
  22. extern void *GetNativeWindowFromWindowRef(WindowRef window);
  23. extern WindowRef GetWindowRefFromNativeWindow(void *window);
  24.  
  25. typedef void (*CopyBitsProcPtr)(const BitMap *srcBits,const BitMap *dstBits,const Rect *srcRect,
  26.                                 const Rect *dstRect,SInt16 mode,RgnHandle maskRgn);
  27.  
  28. void Initialize(void);
  29. static void PowerPlant_CopyBitsPatch(const BitMap *srcBits,const BitMap *dstBits,const Rect *srcRect,
  30.                                 const Rect *dstRect,SInt16 mode,RgnHandle maskRgn);
  31.  
  32. #ifdef __cplusplus
  33. }
  34. #endif
  35.  
  36.  
  37.  
  38. #define kPowerPlantPath        "/System/Library/PrivateFrameworks/PowerPlant.framework/PowerPlant"
  39.  
  40.  
  41.  
  42. CGSConnectionID                gCGSConnID = _CGSDefaultConnection();
  43. CGSConnectionID                gDesktopWindow = NULL;
  44. WindowRef                    gDesktopWindowRef = NULL;
  45. CopyBitsProcPtr                *gPowerPlant_CopyBitsVector = (CopyBitsProcPtr*)FindSymbolAddress(kPowerPlantPath,"_CopyBits");
  46. CopyBitsProcPtr                gPowerPlant_CopyBitsProc = NULL;
  47.  
  48.  
  49.  
  50. void Initialize(void)
  51. {
  52.     SInt32            x,y;
  53.     WindowRef        window;
  54.     Rect            bounds;
  55.     OSStatus        err;
  56.     
  57.     if (gPowerPlant_CopyBitsVector == NULL) {
  58.         fprintf(stderr,"Unresolved vector: PowerPlant_CopyBits");
  59.         return;
  60.     }
  61.     
  62.     gDesktopWindowRef = NULL;
  63.     for (window = GetWindowList();window != NULL;window = MacGetNextWindow(window))
  64.     {
  65.         GetWindowBounds(window,kWindowContentRgn,&bounds);
  66.         if ((gDesktopWindowRef == NULL) ||
  67.             ((bounds.right - bounds.left) > x) ||
  68.             ((bounds.bottom - bounds.top) > y))
  69.         {
  70.             gDesktopWindowRef = window;
  71.             x = bounds.right - bounds.left;
  72.             y = bounds.bottom - bounds.top;
  73.         }
  74.     }
  75.     
  76.     gDesktopWindow = (CGSConnectionID)GetNativeWindowFromWindowRef(gDesktopWindowRef);
  77.     
  78.     err = CGSSetWindowOpacity(gCGSConnID,gDesktopWindow,false);
  79.     if (err != noErr) {
  80.         fprintf(stderr,"CGSSetWindowOpacity failed: %08lX, %s",err,strerror(err));
  81.         return;
  82.     }
  83.     
  84.     err = CGSSetWindowAlpha(gCGSConnID,gDesktopWindow,1.0);
  85.     if (err != noErr) {
  86.         fprintf(stderr,"CGSSetWindowAlpha failed: %08lX, %s",err,strerror(err));
  87.         return;
  88.     }
  89.     
  90.     gPowerPlant_CopyBitsProc = *gPowerPlant_CopyBitsVector;
  91.     *gPowerPlant_CopyBitsVector = &PowerPlant_CopyBitsPatch;
  92.     
  93.     GetWindowBounds(gDesktopWindowRef,kWindowContentRgn,&bounds);
  94.     InvalWindowRect(gDesktopWindowRef,&bounds);
  95.     
  96.     fprintf(stderr,"FinderPatch installed: pid = %d\n",getpid());
  97.     return;
  98. }
  99.  
  100.  
  101.  
  102. void PowerPlant_CopyBitsPatch(const BitMap *srcBits,const BitMap *dstBits,const Rect *srcRect,
  103.                              const Rect *dstRect,SInt16 mode,RgnHandle maskRgn)
  104. {
  105.     SInt32            vdex,hdex;
  106.     CGRect            bounds;
  107.     UInt32            depth;
  108.     SInt32            rows;
  109.     void            *data[2];
  110.     UInt8            *base,*baseAddr;
  111.     PixMap            *pm;
  112.     SInt32            width,height;
  113.     OSStatus        err;
  114.     static            int once = 1;
  115.     
  116.     if (once)
  117.     {
  118.         pm = (PixMap*)srcBits;
  119.         width = pm->bounds.right - pm->bounds.left;
  120.         height = pm->bounds.bottom - pm->bounds.top;
  121.         baseAddr = *(UInt8**)pm->baseAddr;
  122.         rows = pm->rowBytes & 0x3FFF;
  123.         
  124.         for (vdex = 0;vdex < height;vdex += 1)
  125.         {
  126.             base = baseAddr;
  127.             base += rows * vdex;
  128.             
  129.             for (hdex = 0;hdex < width;hdex += 1)
  130.                 ((UInt32*)base)[hdex] = 0x0F010201;
  131.         }
  132.         
  133.         once = 0;
  134.     }
  135.     
  136.     gPowerPlant_CopyBitsProc(srcBits,dstBits,srcRect,dstRect,mode,maskRgn);
  137.     
  138.     // Poor mans transparency hack
  139.     err = CGSLockWindowBits(gCGSConnID,gDesktopWindow,&bounds,&depth,&data[0],&rows);
  140.     if (err != noErr) {
  141.         fprintf(stderr,"CGSLockWindowBits failed: %08lX, %s",err,strerror(err));
  142.         return;
  143.     }
  144.     
  145.     for (vdex = dstRect->top;vdex < dstRect->bottom;vdex += 1)
  146.     {
  147.         base = (UInt8*)data[0];
  148.         base += rows * vdex;
  149.         
  150.         for (hdex = dstRect->left;hdex < dstRect->right;hdex += 1)
  151.             if ((((UInt32*)base)[hdex] & 0x00FFFFFF) == 0x00010201)
  152.                 ((UInt32*)base)[hdex] = 0x0F000000;
  153.     }
  154.     
  155.     err = CGSUnlockWindowBits(gCGSConnID,gDesktopWindow,NULL);
  156.     if (err != noErr) {
  157.         fprintf(stderr,"CGSUnlockWindowBits failed: %08lX, %s",err,strerror(err));
  158.         return;
  159.     }
  160. }
  161.